package hang.xipujewelWeb.web.account;

import hang.xipujewelWeb.entity.account.User;
import hang.xipujewelWeb.entity.app.ExchangeRate;
import hang.xipujewelWeb.entity.app.Jewel;
import hang.xipujewelWeb.entity.app.Ring;
import hang.xipujewelWeb.entity.app.ShoppingCart;
import hang.xipujewelWeb.service.account.ExchangeRateManager;
import hang.xipujewelWeb.service.account.ShoppingCartManager;
import hang.xipujewelWeb.service.account.UserManager;
import hang.xipujewelWeb.utils.UserUtil;

import java.util.List;

import javax.servlet.ServletRequest;

import net.esoar.modules.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;

/**
 * 用户管理
 * 
 * @author qizai
 * 
 */
@Controller
@RequestMapping("ShopCart")
public class ShopCartController extends BaseController<ShoppingCart> {

	@Autowired
	private UserManager userManager;

	@Autowired
	private ShoppingCartManager shoppingCartManager;

	@Autowired
	private ExchangeRateManager exchangeRateManager;

	@RequestMapping("")
	public String list(Model model, ServletRequest request) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		List<ShoppingCart> shoppingCarts = shoppingCartManager.findByUesr(cUser.getId());
		List<Jewel> jewels = Lists.newArrayList();
		List<Ring> rings = Lists.newArrayList();
		ExchangeRate exchangeRate = exchangeRateManager.getAll().get(0);
		float sum = 0;

		for (ShoppingCart shoppingCartsCart : shoppingCarts) {
			if (shoppingCartsCart.getJewel() != null && shoppingCartsCart.getJewel().getId() != 0) {
				Jewel jewel = shoppingCartsCart.getJewel();
				float discount = (float) ((100.0 + jewel.getDiscount() + cUser.getParent().getPriceBaseNumber()
						.getJewelBaseNumBer()) / 100.0);
				float priceRMB = jewel.getWeight() * jewel.getInternationalPrice() * (discount)
						* exchangeRate.getRate();
				jewel.setSecondPrice(priceRMB);//把算好的价格放在非持久化的字段中，传到前端显示
				jewel.setSecondDiscount((discount - 1) * 100);
				jewel.setShopCartId(shoppingCartsCart.getId());
				jewels.add(jewel);
				sum = sum + jewel.getSecondPrice();
			} else if (shoppingCartsCart.getRing() != null && shoppingCartsCart.getRing().getId() != 0) {
				Ring ring = shoppingCartsCart.getRing();
				if (cUser.getType() == 2) {
					float secondPrice = ring.getPrice() * cUser.getParent().getPriceBaseNumber().getRingBaseNumber();
					ring.setSecondPrice(secondPrice);
					sum = sum + ring.getSecondPrice();
				}
				ring.setShopCartId(shoppingCartsCart.getId());
				sum = sum + ring.getPrice();
				rings.add(ring);
			}
		}

		model.addAttribute("jewels", jewels);
		model.addAttribute("rings", rings);
		model.addAttribute("count", shoppingCarts.size());
		model.addAttribute("sum", sum);
		return "shopCart/shopCart-list";
	}

	@RequestMapping("getShopCartCount")
	@ResponseBody
	public String getShopCartCount() {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		List<ShoppingCart> shoppingCarts = shoppingCartManager.findByUesr(cUser.getId());
		if (shoppingCarts != null) {
			return "" + shoppingCarts.size();
		} else
			return "0";
	}

	@RequestMapping("deleteShop")
	@ResponseBody
	public String deleteShop(@RequestParam("id") Long id) {
		shoppingCartManager.delete(id);
		return "删除成功";
	}

	@RequestMapping("deleteShops")
	@ResponseBody
	public String deleteShops(@RequestParam("ids") List<Long> ids) {
		shoppingCartManager.deletes(ids);
		return "删除成功";
	}

	@Override
	public String baseMapping() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModelName() {
		// TODO Auto-generated method stub
		return null;
	}

}
